package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_circle_img;

import cn.com.api.service.ApiU_circle_imgService;

import cn.com.util.*;


public class U_circle_imgBaseController {

	private static Logger logger = Logger.getLogger(U_circle_imgBaseController.class);
	
	@Autowired
	private ApiU_circle_imgService apiU_circle_imgService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "u_circle_img-list")
	public ModelAndView u_circle_img_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_circle_img_list.jsp");
		
		List<ApiU_circle_img> u_circle_imgs = apiU_circle_imgService.findU_circle_imgs();
		mv.addObject("u_circle_imgs", u_circle_imgs);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "u_circle_img-add-pre")
	public ModelAndView u_circle_img_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_circle_img_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiU_circle_img u_circle_img = apiU_circle_imgService.findU_circle_imgById(Integer.valueOf(id));
			mv.addObject("u_circle_img",u_circle_img);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "u_circle_img-add")
	public void u_circle_img_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		String circle_id = request.getParameter("circle_id");
		String img = request.getParameter("img");
		String sort = request.getParameter("sort");
		
		Map<String,Object> map = new HashMap<String,Object>();
		//String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			ApiU_circle_img u_circle_img = new ApiU_circle_img();
            /*
		    u_circle_img.setId(id);
		    u_circle_img.setCircle_id(circle_id);
		    u_circle_img.setImg(img);
		    u_circle_img.setSort(sort);
	        */
			apiU_circle_imgService.insertU_circle_img(u_circle_img);
		}
		//edit
		else{
			ApiU_circle_img u_circle_img = apiU_circle_imgService.findU_circle_imgById(Integer.valueOf(id));
	        /*
		    u_circle_img.setId(id);
		    u_circle_img.setCircle_id(circle_id);
		    u_circle_img.setImg(img);
		    u_circle_img.setSort(sort);
			*/
			apiU_circle_imgService.updateU_circle_img(u_circle_img);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
        JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "u_circle_img-delete")
	public void u_circle_img_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiU_circle_imgService.deleteU_circle_img(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
}



